package com.safe.first.domain.order.factory;

import cn.hutool.extra.spring.SpringUtil;
import com.safe.first.domain.order.constants.OrderType;
import com.safe.first.domain.order.model.Order;
import com.safe.first.domain.user.User;
import lombok.Data;
import org.springframework.stereotype.Component;

/**
 * @author Mickey
 * @since 2022/5/27
 **/
@Component
public class CompositeOrderFactory implements OrderFactory<Order, CompositeOrderFactory.CompositeOrderParam<?>> {

    @Override
    public Order instance(CompositeOrderParam<?> compositeOrderParam, User user) {
        OrderType orderType = compositeOrderParam.getOrderType();
        OrderFactory<Order, Object> factory = SpringUtil.getBean(orderType.getFactory());
        return factory.instance(compositeOrderParam.getData(), user);
    }

    @Data
    public static class CompositeOrderParam<T> {
        private OrderType orderType;
        private T data;
    }
}
